package com.gjy.sql.mysql.advanced;

import com.gjy.sql.mysql.MySQLUtil;
import com.google.common.collect.Lists;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-06-30 10:12:12
 */
public class CursorAdv {

    /**
     * url要添加：useCursorFetch=true
     */
    public List<String> getPigStockCursor() {
        List<String> list = Lists.newArrayList();
        try (Connection connection = MySQLUtil.connection();
             Statement cs = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {

            cs.setFetchSize(5000);
            try (ResultSet rs = cs.executeQuery("select id,hoggery_id,pigsty_id from pig_stock_snapshot_20250617")) {
                while (rs.next()) {
                    String id = rs.getString("id");
                    String hoggeryId = rs.getString("hoggery_id");
                    String pigstyId = rs.getString("pigsty_id");
                    list.add(id + "_" + hoggeryId + "_" + pigstyId);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
